package com.example.qxfw.rcyw.controller;

import com.dmgis.entity.DmMapPoint;
import com.dmgis.weblib.MapCommon;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.DiseaseMonitorInfo;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.common.entity.qx.Doppler;
import com.example.qxfw.common.entity.xt.AdminTable;
import com.example.qxfw.common.entity.yj.*;
import com.example.qxfw.common.util.DmgisUtils;
import com.example.qxfw.mongodb.qx.entity.QxLdPngFile;
import com.example.qxfw.mongodb.qx.service.QxLdPngFileService;
import com.example.qxfw.qxxx.service.DopplerService;
import com.example.qxfw.rcyw.dto.ArgiMoniInfoDto;
import com.example.qxfw.rcyw.dto.StationDto;
import com.example.qxfw.rcyw.service.*;
import com.example.qxfw.ybzz.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.stream.Collectors;

import static com.example.qxfw.common.util.DateUtil.convertDateFormat;

@Api(tags = "日常业务-报警信息接口")
@RestController
@CrossOrigin
@RequestMapping("/bjxx")
public class BjxxController {

    @Autowired
    private AdminTableService adminTableService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private SmallScaleStationService smallScaleStationService;

    @Autowired
    private WarnMoniInfoService warnMoniInfoService;

    @Autowired
    private RadarWarnMonitorInfoService radarWarnMonitorInfoService;

    @Autowired
    private ImpWeatherMoniInfoService impWeatherMoniInfoService;

    @Autowired
    private ThunderMoniInfoService thunderMoniInfoService;

    @Autowired
    private AgriMoniInfoService agriMoniInfoService;

    @Autowired
    private StationInfoService stationInfoService;

    @Autowired
    private DiseaseMonitorInfoService diseaseMonitorInfoService;

    @Autowired
    private DisWeatherMoniInfoService disWeatherMoniInfoService;

    @Autowired
    private WaterWarnMoniInfoService waterWarnMoniInfoService;

    @Autowired
    private DopplerService dopplerService;

    @Autowired
    private QxLdPngFileService qxLdPngFileService;

    @ApiOperation("获取县")
    @GetMapping("/getCounty")
    public SysResult getCounty() {
        List<AdminTable> county = adminTableService.getCounty();
        return SysResult.success(county, county.size());
    }

    @ApiOperation("获取监控模板")
    @GetMapping("/getMonitorTemplates")
    public SysResult getMonitorTemplates(String paramName) {
        List<String> templates = sysConfigService.getMonitorTemplates(paramName);
        return SysResult.success(templates, templates.size());
    }

    @ApiOperation("获取区域自动站点信息")
    @GetMapping("/getSmallScaleStation")
    public SysResult getSmallScaleStation() {
        Map<String, StationDto> map = smallScaleStationService.selectList();
        List<Object> keyList = new ArrayList<>();
        List<Object> valueList = new ArrayList<>();
        Set<Map.Entry<String, StationDto>> entries = map.entrySet();
        for (Map.Entry<String, StationDto> entry : entries) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            keyList.add(key);
            valueList.add(value);
        }
        return SysResult.success(keyList, valueList);
    }

    //监控信息
    @ApiOperation("获取气象要素监控信息")
    @GetMapping("/getElementDataBind")
    public SysResult getElementDataBind(Integer elementHour) {
        List<WarnMonitorInfo> listByTime = warnMoniInfoService.getListByTime(elementHour);
        Map<String, StationDto> map = smallScaleStationService.selectList();
        for (WarnMonitorInfo info : listByTime) {
            info.setLon(map.get(info.getStationid()).getDLon());
            info.setLat(map.get(info.getStationid()).getDLat());
        }
        return SysResult.success(listByTime, listByTime.size());
    }

    @ApiOperation("气象要素监控信息解除")
    @PostMapping("/updateWarnMoni")
    public SysResult updateWarnMoniInfo(String ids) {
        int rows = warnMoniInfoService.updateWarnMonitor(ids);
        return SysResult.success(rows);
    }

    @ApiOperation("获取雷达监控信息")
    @GetMapping("/getRadarDataBind")
    public SysResult getRadarDataBind(Integer radarHour) {
        MapCommon common = new MapCommon();
        List<RadarWarnMonitorInfo> listByTimeAndName = radarWarnMonitorInfoService.selectListByTimeAndName(radarHour);
        for (RadarWarnMonitorInfo info : listByTimeAndName) {
            String newwarnrea = "";
            String warnarea = info.getWarningarea();
            String[] areas = warnarea.split("\\*");//mian
            for (String area : areas) {
                String[] dots = area.split(",");
                DmMapPoint[] polygon = new DmMapPoint[dots.length / 2];
                for (int k = 0; k < dots.length / 2; k++) {
                    DmMapPoint point = DmgisUtils.lbtToWgs(Double.parseDouble(dots[2 * k]), Double.parseDouble(dots[2 * k + 1]));
                    polygon[k] = new DmMapPoint(point.getX(), point.getY());
                }
                int[] parts = {polygon.length};
                int numParts = 1;

                DmMapPoint centerPoint = common.getPolygonCenterPoint(polygon, parts, numParts);
                newwarnrea += centerPoint.getX() + "," + centerPoint.getY() + "*";

            }
            if (newwarnrea.endsWith("*")) newwarnrea = newwarnrea.substring(0, newwarnrea.length() - 1);
            info.setWarningarea(newwarnrea);

            Double[] rect = new Double[4];
            List<Doppler> dopplerList = dopplerService.selectDopplerByPath(info.getFilepath());
            List<QxLdPngFile> qxLdPngFileList = new ArrayList<>();
            dopplerList.forEach(dop -> {
                rect[0] = dop.getWest();
                rect[1] = dop.getSouth();
                rect[2] = dop.getEast();
                rect[3] = dop.getNorth();
                String ldtype = dop.getLdtype();
                String elevation = dop.getElevation().toString().equals("0.0") ? "000" : dop.getElevation().toString();
                String stationid = dop.getStationid();
                String timechar = dop.getTimechar();
                String datechar = dop.getDatechar().toString();
                String dateTime = convertDateFormat(datechar);
                assert dateTime != null;
                String[] parts = dateTime.split("-");
                String dateyear = parts[0];
                List<QxLdPngFile> selectIdDateCharTimeChar = qxLdPngFileService.findByDateCharAndTimeCharAndStationIdAndElevationAndLdTypeAndDateYear(dateTime, timechar, stationid, elevation, ldtype, dateyear);

                qxLdPngFileList.addAll(selectIdDateCharTimeChar);

            });

            List<QxLdPngFile> qxLdPngFiles = qxLdPngFileList.stream().distinct().collect(Collectors.toList());
            qxLdPngFiles.forEach(q -> {
                String q_id = q.get_id();
                String datechar = q.getDatechar();
                String timechar = q.getTimechar();
                String hours = timechar.substring(0, 2);
                String minutes = timechar.substring(2);
                info.setImgId(q_id);
                info.setImgName(datechar + " " + hours + ":" + minutes + ":00");
                info.setImgRect(rect);
            });

        }
        return SysResult.success(listByTimeAndName, listByTimeAndName.size());
    }

    @ApiOperation("雷达监控解除")
    @PostMapping("/removeRadarMonitor")
    public SysResult removeRadarMonitor(String filepaths) {
        Integer rows = radarWarnMonitorInfoService.update(filepaths);
        return SysResult.success(rows);
    }

    @ApiIgnore
    @ApiOperation("获取重要天气监控信息")
    @GetMapping("/getImportWeatherDataBind")
    public SysResult getImportWeatherDataBind(Integer impHour) {
        List<ImpWeatherMoniInfo> list = impWeatherMoniInfoService.getMonitorWeather(impHour);
        return SysResult.success(list, list.size());
    }

    @ApiOperation("获取雷电监控信息")
    @GetMapping("/getRadenDataBind")
    public SysResult getRadenDataBind(Integer thunderAutoHour) {
        List<ThunderMoniInfo> list = thunderMoniInfoService.getThunderMonitor(thunderAutoHour);
        return SysResult.success(list, list.size());
    }

    @ApiOperation("雷电监控信息解除")
    @PostMapping("/updateRadenData")
    public SysResult updateRadenData(String ids) {
        int rows = thunderMoniInfoService.updateThunderMonitor(ids);
        return SysResult.success(rows);
    }

    @ApiOperation("获取农情监控信息")
    @GetMapping("/getAgrDataBind")
    public SysResult getAgrDataBind(Integer agriHour) {
        //重写查询方法，返回一个对象给前端
        List<ArgiMoniInfoDto> monitorArgiXwx = agriMoniInfoService.getMonitorArgiXwx(agriHour);
        List<StationInfo> list2 = stationInfoService.list();
        Map<String, StationDto> map = smallScaleStationService.selectList();
        for (ArgiMoniInfoDto info : monitorArgiXwx) {
            for (StationInfo stainfo : list2) {
                if (stainfo.getStationid().equals(info.getStationid())) {
                    info.setLon(map.get(stainfo.getSmallscalestationid()).getDLon());
                    info.setLat(map.get(stainfo.getSmallscalestationid()).getDLat());
                    break;
                }
            }
        }
        return SysResult.success(monitorArgiXwx, monitorArgiXwx.size());
    }

    @ApiOperation("农情监控信息解除")
    @PostMapping("/updateAgrData")
    public SysResult updateAgrData(String ids) {
        int rows = agriMoniInfoService.updateMonitorArgi(ids);
        return SysResult.success(rows);
    }

    @ApiIgnore
    @ApiOperation("获取农业病虫害监控信息")
    @GetMapping("/getAgrDisaDataBind")
    public SysResult getAgrDisaDataBind(Integer argDiseasAutoHour) {
        List<DiseaseMonitorInfo> list1 = diseaseMonitorInfoService.getMonitorDisease(argDiseasAutoHour);
        List<StationInfo> list2 = stationInfoService.list();
        return SysResult.success(list1, list2);
    }

    @ApiIgnore
    @ApiOperation("农业病虫害监控信息解除")
    @PostMapping("/updateAgrDisaData")
    public SysResult updateAgrDisaData(@RequestBody DiseaseMonitorInfo diseaseMonitorInfo) {
        int rows = diseaseMonitorInfoService.updateMonitorDisease(diseaseMonitorInfo);
        return SysResult.success(rows);
    }

    @ApiIgnore
    @ApiOperation("获取灾害性天气监控信息")
    @GetMapping("/getDisaWeaDataBind")
    public SysResult getDisaWeaDataBind() {
        List<DisWeatherMoniInfo> list = disWeatherMoniInfoService.getMonitorDisWeather();
        return SysResult.success(list, list.size());
    }

    @ApiIgnore
    @ApiOperation("获取水务水情监控信息")
    @GetMapping("/getWaterDataBind")
    public SysResult getWaterDataBind(Integer waterAutoHour) {
        List<WaterWarnMoniInfo> list = waterWarnMoniInfoService.getWaterWarnMonitor(waterAutoHour);
        return SysResult.success(list, list.size());
    }

    @ApiIgnore
    @ApiOperation("水务水情监控信息解除")
    @PostMapping("/updateWaterWarnMonitor")
    public SysResult updateWaterWarnMonitor(@RequestBody WaterWarnMoniInfo waterWarnMoniInfo) {
        int rows = waterWarnMoniInfoService.updateWaterMonitor(waterWarnMoniInfo);
        return SysResult.success(rows);
    }


}
